package com.wuchao.reflect;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Author wuchao66
 * @Date 2023/10/31 9:27
 * @PackageName:com.wuchao.reflect
 * @ClassName: T
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class T {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        //拿到反射目标类的影子
        Class s1 =Class.forName("com.wuchao.reflect.Student");
        Class<? extends Student> s2 = new Student().getClass();
        Class<Student> s3 = Student.class;//超哥最爱
      //  System.out.println(s1==s2);
        //真正的实例化
        Student s  = new Student();
        if(s instanceof  Student){
            //System.out.println("true");
        }
       // System.out.println(s1.isInstance(Student.class));
        //s1-s3还是类级别 s才是实例化
      // s== s3.newInstance().  s==sss
        Student sss = s3.newInstance();//反射实例化
        //拿到属性
        Field[] declaredFields = s3.getDeclaredFields();
        System.out.println("所有属性");
        Arrays.stream(declaredFields)
                .forEach(xjj-> System.out.println(xjj.getName()));
        Field[] declaredFields1 = s3.getFields();
        System.out.println("公有属性");
        Arrays.stream(declaredFields1)
                .forEach(xjj-> System.out.println(xjj.getName()));
        Field age = s3.getDeclaredField("age");
       // sss.age=3;
        //拿到所有的构造函数
        System.out.println("suoyou 构造函数");
        System.out.println("名字\n入参类型\n入参个数");
        Constructor<?>[] declaredConstructors =
                s3.getDeclaredConstructors();
        Arrays.stream(declaredConstructors).forEach(xjj->{
            System.out.println(xjj.getName());
            Arrays.stream(xjj.getParameterTypes())
                    .forEach(xjb-> System.out.println(xjb.getName()));
            System.out.println("入参个数："+xjj.getParameterCount());
        });

        //方法最难的
        System.out.println("名字\n入参类型\n入参个数\n返回值");
        Method[] declaredMethods = s3.getDeclaredMethods();
        Arrays.stream(declaredMethods)
                .forEach(xjj->{
                    System.out.println(xjj.getName());
                    Arrays.stream(xjj.getParameterTypes())
                            .forEach(xjb-> System.out.println(xjb.getName()));
                    System.out.println("入参个数："+xjj.getParameterCount());
                    System.out.println(xjj.getReturnType().getName());
                });
        Method show = s3.getDeclaredMethod("show", String.class);
        s.show("我是真正的本体实例化");
        //反射
        //sss.show("我是反射的的实例化");
        show.invoke(sss,"反调反射实例化");
    }
}
